/**
 * Copyright &copy; 2020 <a href="https://github.com/somowhere/exam-system">exam-system</a> All rights reserved.
 */
package com.shanhesoft.java.modules.exam.web;


import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.ApiOperation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanhesoft.java.common.core.annotation.AnonymousAccess;
import com.shanhesoft.java.common.core.constant.CommonConstants;
import com.shanhesoft.java.common.core.util.DateUtil;
import com.shanhesoft.java.common.core.util.Result;
import com.shanhesoft.java.common.core.vo.PageModel;
import com.shanhesoft.java.common.data.util.QueryWrapperUtil;
import com.shanhesoft.java.common.log.annotation.LogOperate;
import com.shanhesoft.java.common.log.enums.BusinessType;
import com.shanhesoft.java.common.security.util.SecurityUtil;
import com.shanhesoft.java.common.web.resource.BaseResource;
import com.shanhesoft.java.modules.exam.domain.dto.CourseQueryCriteria;
import com.shanhesoft.java.modules.exam.domain.dto.PaperDto;
import com.shanhesoft.java.modules.exam.domain.dto.PaperQueryCriteria;
import com.shanhesoft.java.modules.exam.service.PaperService;

import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Set;

/**
 * 试卷管理Controller 试卷管理
 * @author admin
 * @version 2020-09-26 16:09:29
 */
@RestController
@RequestMapping(value = "${application.admin-path}/exam/paper")
@Slf4j
@AllArgsConstructor
public class PaperResource extends BaseResource {

	private final PaperService service;

	/**
	 * @param id
	 * @return
	 */
	@GetMapping(CommonConstants.URL_ID_REGEX)
	@PreAuthorize("@pms.hasPermission('exam_paper_view')")
	public Result get(@PathVariable String id) {
		log.debug("REST request to get Entity : {}", id);
		//return  Result.buildOkData(service.getOneDto(id));
		return  Result.buildOkData(service.findUserVoById(id));
	}
	/**
	 * GET / : get all paper.
	 *
	 * @param pm the pagination information
	 * @return the Result with status 200 (OK) and with body all paper
	 */

	//@PreAuthorize("@pms.hasPermission('exam_paper_view')")
	@GetMapping
	@ApiOperation(value = "试卷管理查看")
	public Result getPage(PageModel pm, PaperQueryCriteria paperQueryCriteria) {
		return Result.buildOkData(service.findPage(pm, paperQueryCriteria));
	}

	/**
	 * 获取首页热门数据
	 *
	 * @return
	 * @author
	 * @date
	 */
	@AnonymousAccess
	@GetMapping("/getHomePaperPage")
	@ApiOperation(value = "获取首页热门数据", notes = "获取首页热门数据")
	public Result getHomePaperPage() {
		PageModel pm = new PageModel();
		pm.setCurrent(1);
		pm.setSize(4);
		PaperQueryCriteria paperQueryCriteria = new PaperQueryCriteria();
		paperQueryCriteria.setType("0,1");
		return Result.buildOkData(service.findPage(pm, paperQueryCriteria));
	}


	/**
	 * POST / : Save a paperDto.
	 *
	 * @param paperDto the HTTP paper
	 */
	@PreAuthorize("@pms.hasPermission('exam_paper_edit')")
	@ApiOperation(value = "试卷管理编辑")
	@PostMapping
	public Result save(@Valid @RequestBody PaperDto paperDto) {
		log.debug("REST request to save PaperDto : {}", paperDto);
		service.saveOrUpdate(paperDto);
		return Result.buildOk("保存试卷管理成功");

	}

	/**
	 * DELETE //:ids : delete the "ids" Paper.
	 *
	 * @param ids the id of the paper to delete
	 * @return the Result with status 200 (OK)
	 */
	@PreAuthorize("@pms.hasPermission('exam_paper_del')")
	@ApiOperation(value = "试卷管理删除")
	@DeleteMapping
	public Result delete(@RequestBody Set<String> ids) {
		log.debug("REST request to delete Paper: {}", ids);
		service.removeByIds(ids);
		return Result.buildOk("删除试卷管理成功");
	}

	/**
	 * 获取服务器当前时间
	 *
	 * @return
	 * @author
	 * @date
	 */
	@AnonymousAccess
	@GetMapping("/currentTime")
	@ApiOperation(value = "获取服务器当前时间", notes = "获取服务器当前时间")
	public Result currentTime() {
		return Result.buildOkData(DateUtil.format(LocalDateTime.now(), DateUtil.TIME_FORMAT));
	}

	/**
	 * 获取首页统计数据
	 *
	 * @return
	 * @author
	 * @date
	 */
	@AnonymousAccess
	@GetMapping("/getCountInfo")
	@ApiOperation(value = "获取首页统计数据", notes = "获取首页统计数据")
	public Result getCountInfo() {
		return Result.buildOkData(service.getCountInfo());
	}
}
